package com.woniu.rabbit.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.woniu.common.utils.JWTUtil;
import com.woniu.common.utils.RabbitMsgUtil;
import com.woniu.common.utils.RabbitSendMessage;
import com.woniu.common.utils.ResponseData;

import com.woniu.rabbit.feign.OrderFeign;
import com.woniu.rabbit.feign.ProductFeign;
import com.woniu.common.dto.ForOrderProductDTO;
import com.woniu.rabbit.service.ZyOrderService;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.woniu.common.constant.FinanceConstant.Exchange.DLX_EXCHANGE;
import static com.woniu.common.constant.FinanceConstant.Exchange.EXCHANGE;
import static com.woniu.common.constant.FinanceConstant.RoutingKey.*;

/**
 * @author ltyipx
 * @description 针对表【zy_order】的数据库操作Service实现
 * @createDate 2024-08-07 09:44:03
 */
@Service
public class ZyOrderServiceImpl implements ZyOrderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OrderFeign orderFeign;

    @SneakyThrows
    @Override
    public ResponseData addOrder(List<ForOrderProductDTO> productDTO) {
        //获取请求头中的user信息
        String jwt = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if("jwt".equals(cookie.getName())){
                jwt = cookie.getValue();
            }
        }
        Map<String, Object> userInfo = JWTUtil.getUserInfo(jwt);
        String userId = userInfo.get("id").toString();
        //创建订单id
        long orderId = IdUtil.getSnowflake().nextId();
        //走中移系统
        //发订单队列，创建订单和订单详情
        Map<String, Object> OrderMap = new HashMap();

        OrderMap.put("products", productDTO);
        OrderMap.put("userId", userId);
        OrderMap.put("orderId", String.valueOf(orderId));
        RabbitSendMessage.send(stringRedisTemplate, rabbitTemplate, EXCHANGE, ORDER_ROUTING_KEY, JSON.toJSONString(OrderMap));
        //远程调用库存接口，减库存
        Map<String, Object> ProductMap = new HashMap();
        ProductMap.put("products", productDTO);
        RabbitSendMessage.send(stringRedisTemplate, rabbitTemplate, EXCHANGE, PRODUCT_ROUTING_KEY, JSON.toJSONString(ProductMap));

        //发送延时队列，15分钟后，若用户没有付款，则取消订单
        Map<String, Object> ttlMap = new HashMap();
        ttlMap.put("orderId", String.valueOf(orderId));
        ttlMap.put("userId",userId);
        RabbitSendMessage.send(stringRedisTemplate,rabbitTemplate,EXCHANGE,TTL_ORDER_ROUTING_KEY,JSON.toJSONString(ttlMap));

        Thread.sleep(900);
        Boolean flag = orderFeign.queryOrderIfExist(String.valueOf(orderId));
        if(flag){
            return new ResponseData().success(String.valueOf(orderId));
        }else {
            return new ResponseData().fail();
        }

    }


}





